<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<% String path=request.getContextPath(); String basePath=request.getScheme()+ "://" + request.getServerName() + ":" + request.getServerPort() + path+ "/"; %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
<html  xmlns="http://www.w3.org/1999/xhtml"> 
<head>
<base href="<%=basePath%>">
<title>菜单管理-添加/编辑</title>
	<%@ include file="/WEB-INF/jsp/common/common.jsp" %>
	<script src="<%=path%>/js/jquery.divbox.js" type="text/javascript"></script>
	<script src="js/json2.js" type="text/javascript"></script>
	<script type="text/javascript">

	var riskMainType = "";//全局的产品大类字段。新增时在配置第一个子二维码时赋值，编辑时由已配置二维码带出。
	var type = "${type}";//操作类型，有edit和add两种
	var codeId = "";
	var menuId = "${menuId}";
	var menuSort = "${menuSort}";
	$(document).ready(function() {
		if(type == 'edit'){
			codeId = "${codeId}";
			//加载已配置列表
			queryConfigedScanCode(1);
			//二维码名称及描述置灰
			$("#menuName").attr("disabled",true);
			$("#menuName").css("background-color","#CCC");
			$("#menuDesc").attr("disabled",true);
			$("#menuDesc").css("background-color","#CCC");
			$("#menuSort").attr("disabled",true);
			$("#menuSort").css("background-color","#CCC");
			$("#scanCodeSaveButton").attr("disabled","true");
			$("#menuName").val("${menuName}");
			$("#menuDesc").val("${menuDesc}");
			$("#menuSort").val("${menuSort}");
		}else if(type=='add'){

			$("#scanCodebutton").attr("display","none");
			$("#scanCodeSaveButton").attr("display","");
		}
	});


	function addNewMenu(){
		var menuName = $.trim($('#menuName').val());
		var menuDesc = $.trim($('#menuDesc').val());
		var menuSort = $.trim($('#menuSort').val());
		if(menuName == null || menuName.length == 0){
			$.message("error","请录入菜单名称。")
			return ;
		}
		$.openLoading();
		$.ajax({
			url : "<%=path%>/unionScanMenuConfig/addNewMenu",
			type : 'POST',
			async : true,
			data:{menuName:menuName,menuDesc:menuDesc,menuSort:menuSort},
			dataType:'json',
			success:function(rs){
				/* $.closeLoading(); */
				if(!rs.result){
					$.message("fail","保存失败！请联系管理员");
				}else{
					var id = rs.data;
					$.message("success","新增保存成功，可以配置关联子菜单");
					setTimeout(function(){
						window.location.href="<%=path%>/unionScanMenuConfig/edit?menuId="+id + "&menuName=" + encodeURI(encodeURI(menuName)) + "&menuDesc=" + encodeURI(encodeURI(menuDesc)) + "&menuSort=" + menuSort;
					},"1500")
				}
			}
		});
	}

	//---------------------------------------子二维码配置窗体相关方法 begin --------------------------------------
	//展示选择子二维码窗体
	function openScanCodeDiv(){
		if(type=='add'){

			$.message("warn","请先保存该聚合二维码后再配置子二维码");

			return;
		}

		$("#scanCodeDiv").OpenDiv();
		queryScanCode(1);
	}

	//拼接查询合作伙伴 字符串
	function queryScanCode(pageIndex){
		//规则id
		var ruleId = $.trim($('#ruleId').val());
		//代理人编码
		var agentId = $.trim($('#agentId').val());
		//方案号
		var planNo = $.trim($('#planNo').val());

		loadScanCode({current:pageIndex,size:8,ruleId:ruleId,agentId:agentId,planNo:planNo,riskMainType:riskMainType,unionCodeId:codeId,menuId:menuId});
	}

	//关闭选择子二维码窗体
	function closeScanCodeDiv(){
		$("#scanCodeDiv").CloseDiv();
	}

	//查询待配置的二维码列表
	function loadScanCode(param){
		$.openLoading();
		$.ajax({
			url : "<%=path%>/unionScanMenuConfig/queryScanCode?"+new Date().getTime(),
			type : 'POST',
			async : true,
			data:param,
			dataType:'json',
			success:function(page){
				$.closeLoading();
				if($.validate(page)){
					return;
				}
				var checkboxObj ="<input class='input_fxk' name='checkboxScanCode' type='checkBox' value=''>";
				var fieldNames = "id@TDH,rule_info,agent_no,plan_no,status";
				var tableContent = creatTable1(fieldNames,page,checkboxObj);
				var tableObj = $("#scanCodeTable");
				tableObj.find("tbody").remove();
				tableObj.append(tableContent);
				var strPage = creatChannelPage2(param.current,6,page.maxPage,"108");
				var pageDIV = $("#scanCodePageDiv");
				pageDIV[0].innerHTML="";
				pageDIV.append(strPage);
				$('#channelInfoDiv').OpenDiv();
				bindradio();
			}
		});


	}
	//---------------------------------------子二维码配置窗体相关方法 begin --------------------------------------

	//加载已配置的子二维码列表：
	function queryConfigedScanCode(pageIndex){
		loadConfigedScanCode(1);
	}

	function loadConfigedScanCode(pageIndex){
		var param = {current:pageIndex,size:8,ruleId:codeId,menuId:menuId};
		$.ajax({
			url : "<%=path%>/unionScanMenuConfig/queryConfigedScanCode",
			type : 'POST',
			async : true,
			data:param,
			dataType:'json',
			success:function(page){
				$.closeLoading();
				if($.validate(page)){
					return;
				}
				//var checkboxObj ="<input class='input_fxk' name='checkboxScanCode' type='checkBox' value=''>";
				var lastbutton = "<a href='javascript:void(0)' onClick='deleteConfigedUnionCode(this);return false'>删除</a>";
				var fieldNames = "ID@TDH,RULE_NAME,RULE_AGENT_DESC,AGENT_ID,status";
				var tableContent = creatTable(fieldNames,page,lastbutton);
				var tableObj = $("#configedUnionQRCodeTable");
				tableObj.find("tbody").remove();
				tableObj.append(tableContent);
				var strPage = creatChannelPage2(param.current,6,page.maxPage,"107");
				var pageDIV = $("#configedUnionQRCodeDiv");
				pageDIV[0].innerHTML="";
				pageDIV.append(strPage);
				bindradio();
			}
		});
	}

	//绑定单选按钮事件，选中某个单选按钮，该按钮所在行高亮显示
	function bindradio(){
		$("input[name='radioNlifePlan']").click(function(){
			$("input[name='radioNlifePlan']").parent().parent().removeClass("table_highlighter");
			$(this).parent().parent().addClass("table_highlighter");
       	});
		$("input[name='radioChannelInfo']").click(function(){
			$("input[name='radioChannelInfo']").parent().parent().removeClass("table_highlighter");
			$(this).parent().parent().addClass("table_highlighter");
       	});
	}


	function deleteConfigedUnionCode(obj){
		var editTr = $(obj).parent('td').parent('tr')[0];
		var codeId = editTr.childNodes[0].innerHTML;
		$.openLoading();
		$.ajax({
			url : "<%=path%>/unionScanMenuConfig/deleteConfigedUnionCode",
			type : 'POST',
			async : true,
			data:{codeId:codeId,menuId:menuId},
			dataType:'json',
			success:function(){
				/* $.closeLoading(); */
				$.message("success","删除成功");
				setTimeout(function(){
					window.location.reload();
				},"2000")
			}
		});
	}

	//新增后跳转列表
	function saveNewUnionCode(){
		var codeName = $.trim($('#menuName').val());
		var codeDesc = $.trim($('#menuDesc').val());
		$.openLoading();
		$.ajax({
			url : "<%=path%>/unionScanMenuConfig/saveNewUnionCode",
			type : 'POST',
			async : false,
			data:{codeName:codeName,codeDesc:codeDesc},
			dataType:'json',
			success:function(){
				$.closeLoading();
				$.message("success","新增成功，跳转列表");
				window.location.href='<%=path%>/unionScanManger/list';
			}
		});
	}
	//保存checked的二维码关联
	function saveChecked(){
		$.openLoading();
		var ids = '';
		var names = '';
		$("input[name='checkboxScanCode']:checked").each(
		 	function(){
		 		var name = $(this).parent().parent().children("td:eq(2)").html();
		 		//console.log(this);
		 		var id = $(this).parent().parent().children("td:eq(1)").html();
		 		//console.log(id);
		 		ids += id + ',';
		 		names += name + ','
		 	});
		if(ids.length == 0){
			$.message("warn","未选择");
		}
		ids.slice(0,-1); //去掉最后多的一个，
		names.slice(0,-1); //去掉最后多的一个，
		$.ajax({
			url : "<%=path%>/unionScanMenuConfig/saveChecked",
			type : 'POST',
			async : false,
			data:{ids:ids,unionCodeId:codeId,names:names,menuId:menuId},
			dataType:'json',
			success:function(data){
				/* $.closeLoading(); */
				$.message(data.result,data.message);
				setTimeout(function(){
					window.location.reload();
				},"2000")
			}
		});
	}
	</script>
</head>
<body >
	<div class="current">
           <table class="current_table_1">
              <tbody>
                <tr class="f2">
                  <td>
                    <img src="./images/current_1.jpg">
                  </td>
                  <td class="current_table_1_td">
                    <img src="images/current_1.png">&nbsp;您现在的位置：&nbsp;
                      <span style=" color:#136bab; ">界面出单管理
                      <span style="padding:0 5px; font-weight:bold; line-height:28px;">&gt;</span>菜单管理
                  </span>
                  </td>
                  <td><img src="./images/current_3.jpg"></td>
              </tr>
           </tbody>
         </table>
      </div>

		<table class="table_02" id="table">
			<tr>
				<td>
					<table class="search_table">
						<tr height="30px">
						 	<td  style="width:'20%' height:'30';text-align: center;">
								菜单名称：<input id="menuName" class="input_text">
	                  	        菜单描述：<input id="menuDesc" type="text" class="input_text">
								序号：<input id="menuSort" type="text" class="input_text">
	                  	        
	                  	        <input type="button"  class="input_button_2" id="scanCodebutton"  value="关联方案" onClick="openScanCodeDiv()"/>&nbsp;&nbsp;
	                  	        <input type="button"  class="input_button_2" id="scanCodeSaveButton"  value="保存" onClick="addNewMenu()"/>&nbsp;&nbsp;
							</td>
						</tr>
						<tr height="30px">
						</tr> 
					</table>
				</td>
			</tr>
			
			<tr>
				<td>
					<table id="configedUnionQRCodeTable" class="content_table_1">
						<thead>
							<tr>
								已关联方案列表
							</tr>
							<tr class="f1">
								<th>规则</th>
								<th>二维码描述</th>
								<th style="width: 20%">代理人编码</th>
								<th style="width: 10%">状态</th>
								<th>操作</th>
							</tr>
						</thead>
						<tbody>
						</tbody>
					</table>
					<div id="configedUnionQRCodeDiv"  class="page"></div>
				</td>
			</tr>
		</table>
	<!-- begin ------------------------ 配置子二维码 DIV 默认隐藏--------------------------->
	<!-- 选择二维码 div-->
  	<div id="scanCodeDiv" style="display:none;width:90%;text-align: center;">
		<div class="table_title_div">
				<table class="table_title" style='width:100%;height: 20px;'>
					<tr>
					<td width="10%"></td>
					<td width="80%" id="interfaceName"></td>
					<td width="10%" align="right"><img src="./images/closedown.gif" alt="关闭"  onclick="closeScanCodeDiv()"/></td>
					</tr>
				</table>
		</div>
		<table class="table_02" style="width:98%">
			<tr>
				<td>
					<table class="search_table">
						<tr height="30px">
							<td colspan="3" style="text-align:center">
								规则ID<input name="txt3"  id="ruleId" class="input_text" />&nbsp;&nbsp;
								代理人编码：<input name="txt3" id="agentId" class="input_text" />&nbsp;&nbsp;
								方案号：<input name="txt3" id="planNo" class="input_text" />&nbsp;&nbsp;
								<input type="button"  class="input_button_1" value="查询" onClick="queryScanCode(1)"/>
							</td>
						</tr>
						<tr>
							*同一聚合二维码下一般只能关联同一产品大类的子二维码。当配置过第一个子二维码后配置固定为该产品大类。
						</tr>
					</table>
				</td>
			</tr>
			<tr>
				<td>
					<table id="scanCodeTable"  class="content_table_1">
						<thead>
							<tr class="f1">
			                  <th style="width:2%">选择</th>
			                  <th style="width:15%">规则</th>
			                  <th style="width:15%">代理人编码</th>
			                  <th style="width:5%">方案号</th>
			                  <th style="width:5%">状态</th>
			              	</tr>
						</thead>
						<tbody>
						</tbody>
					</table>
					<div id="scanCodePageDiv"  class="page"></div>
				</td>
			</tr>
		</table>
		<div align="center">
		   <input type="button" value="保存" onclick="saveChecked()" class="input_button_3"/>&nbsp;&nbsp;&nbsp;
		   <input type="button" value="取消" onclick="closeScanCodeDiv()" class="input_button_3"/>
		</div>
	</div>
<!-- end ------------------------出单机构 DIV 默认隐藏--------------------------->	
</body>
</html>